package com.tongji;

public class ByteTools {
	static int byte2Int(byte[] val) {
		return byte2Int(val, 0);
	}
	
	static int byte2Int(byte[] val, int pos) {
		int ret = 0;
		for (int i = 0; i < 4; i++) {
			ret |= (val[pos + i] & 0xFF) << (3-i)*8;
		}
		return ret;
	}
	static short byte2Short(byte[] val) {
		return byte2Short(val, 0);
	}
	
	static short byte2Short(byte[] val, int pos) {
		short ret = 0;
		for (int i = 0; i < 2; i++) {
			ret |= (val[pos + i] & 0xFF) << (1-i) * 8;
		}
		return ret;
	}
	
	static void int2Byte(int val, byte[] ret) {
		int2Byte(val, ret, 0);
	}
	
	static void int2Byte(int val, byte[] ret, int pos) {
		for (int i = 0; i < 4; i++) {
			ret[pos + i] = (byte)((val >> (3-i)*8) & 0xFF);
		}
	}
	
	public static void short2Byte(short val, byte[] ret) {
		short2Byte(val, ret, 0);
	}
	
	public static void short2Byte(short val, byte[] ret, int pos) {
		for (int i = 0; i < 2; i++) {
			ret[pos + i] = (byte)((val >> (1-i)*8) & 0xFF);
		}
	}
	
	static String int2Ip(int ip) {
		int b0, b1, b2, b3;
		b0 = ip & 0xFF;
		b1 = (ip >> 8) & 0xFF;
		b2 = (ip >> 16) & 0xFF;
		b3 = (ip >> 24) & 0xFF;
		return String.format("%d.%d.%d.%d", b3, b2, b1, b0);
	}
	
	static int RGBToARGB(byte[] rgb, int pos) {
		int ret = 0;
		for (int i = 0; i < 3; i++) {
			ret |= (rgb[pos + i] & 0xFF) << i * 8;
		}
		return ret;
	}
}
